package com.itheima.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class QiniuUtil {
    //空间免费域名 ： http://q8efoqs2v.bkt.clouddn.com/3bd90d2c-4e82-42a1-a401-882c88b06a1a2.jpg

    private static Configuration cfg = new Configuration(Region.huadong());// 替换成你的 //你要把图片传到那个区域（你新建图片存储空间的选的区域）
    private static String accessKey = "LrHyozkcfGJaINX255B3vGjUZ1PrH11kfbDkMGyW";// 替换成你的
    private static String secretKey = "mkY5_ALdVCcPU8VzaOr1O4DzXwU4LDoaDugn_vZF";// 替换成你的
    private static  String bucket = "itheima95";//空间名称 替换成你的


    public static void batchDelete(String[] keyList){
        if(keyList != null && keyList.length > 1000){
            throw  new IllegalArgumentException("单次批量请求的文件数量不得超过1000");
        }
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            //单次批量请求的文件数量不得超过1000
            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            batchOperations.addDeleteOp(bucket, keyList);
            Response response = bucketManager.batch(batchOperations);
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
            for (int i = 0; i < keyList.length; i++) {
                BatchStatus status = batchStatusList[i];
                String key = keyList[i];
                System.out.print(key + "\t");
                if (status.code == 200) {
                    System.out.println("delete success");
                } else {
                    System.out.println(status.data.error);
                }
            }
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
    }

    /**
     * 文件上传
     * @param file 文件类型 字节数组
     * @param fileName 文件名称
     */
    public static void upload(byte[] file,String fileName){
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(file, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }


    /**
     *  文件删除
     * @param fileName 要删除的文件名
     */
    public static void delete(String fileName){
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, fileName);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}
